import { closeLoading, showLoading } from "@/utils";
import { defineStore } from "pinia";
import Router from "@/router";
import { IReadData } from "@/utils/kiosk"
import { IUser } from "@/api/typing";
import { getStorage, removeStorage } from "@/utils/localStorage";
import { RouteLocationNormalized } from "vue-router";
import { IS_DEV } from "@/config";
import { setStorage } from '@/utils/localStorage';

interface IState {
  cardInfo:any | null,
  user: IUser | null,
  tempUser: IUser | null,
}

export const useUserStore = defineStore({
  id: 'user',
  state: (): IState => ({
    cardInfo:{},
    user: null,
    /** 临时用户，用于绑定手机号 */
    tempUser: null,
  }),
  getters: {

  },
  actions: {
    getUser() {
      return this.user
    },
    setCardInfo(cardInfo:any){
      if(IS_DEV){
        // 开发环境暂存读卡信息
        setStorage("dev_card_info_data",cardInfo)
      }
      this.$patch((state) => {
        state.cardInfo = cardInfo;
      })
    },
    setUser(user: IUser) {
      if(IS_DEV){
        // 开发环境暂存user
        setStorage("dev_user_data",user)
      }
      this.$patch((state) => {
        state.user = user
      })
      const temp_route: RouteLocationNormalized = getStorage("temp_route")
      if (temp_route) {
        // removeStorage("temp_route")
        Router.replace({
          path: temp_route.path,
          query: temp_route.query
        })
      }
    },
    setTempUser(user: IUser | null) {
      this.$patch((state) => {
        state.tempUser = user
      })
    },
    /** 获取临时user数据 */
    getTempUser(){
      return this.tempUser
    },
    clear() {
      this.$patch((state) => {
        state.user = null
        state.cardInfo={};
      })
    }
  }
})